package GUI;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.border.LineBorder;

import Joc.Joc;
import Joc.JocStates;
import Model.EroarePuzzleMessagesLitere;
import Model.HandlerMessagesPuzzle;

public class IntroducereDatePuzzle {
	JFrame frame;
	JPanel panelIntroducereDatePuzzle;
	JButton buttonSave;
	JButton buttonCancel;
	JTable tabel;
	JLabel label;
	public static ArrayList<String> date;

	public IntroducereDatePuzzle() {
		frame=new JFrame("Introducere Date Puzzle");
		panelIntroducereDatePuzzle = new JPanel();
		panelIntroducereDatePuzzle.setLayout(null);

		tabel = new JTable(new TabelModelPuzzle());
		tabel.setBounds(50, 30, 150, 50);
		tabel.setBorder(new LineBorder(Color.BLACK));
		panelIntroducereDatePuzzle.add(tabel);

		label = new JLabel("De exemplu :");
		label.setBounds(10, 90, 80, 25);
		panelIntroducereDatePuzzle.add(label);

		ImageIcon newIcon = new ImageIcon("exemplu_puzzle.png");
		label = new JLabel();
		label.setIcon(newIcon);
		label.setBounds(90, 95, 130, 100);
		panelIntroducereDatePuzzle.add(label);

		buttonSave = new JButton("Save");
		buttonSave.setBounds(110, 200, 70, 25);
		buttonSave.addActionListener(new Action());
		panelIntroducereDatePuzzle.add(buttonSave);
		

		buttonCancel = new JButton("Cancel");
		buttonCancel.setBounds(200, 200, 80, 25);
		buttonCancel.addActionListener(new Action());
		panelIntroducereDatePuzzle.add(buttonCancel);

		frame.add(panelIntroducereDatePuzzle);
		frame.setSize(300, 300);
		frame.setVisible(true);
		frame.setResizable(false);
	}

	public ArrayList<String> getDataFromTable() {
		ArrayList<String> listaDataTable = new ArrayList<String>();
		for (int i = 0; i < tabel.getColumnCount(); i++)
			for (int j = 0; j < tabel.getRowCount(); j++) {
				String nr = tabel.getValueAt(i, j).toString();
				listaDataTable.add(nr);
			}

		return listaDataTable;

	}

	class Action implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == buttonCancel){
				frame.dispose();
				MassagePanel.clearMessagesPanel();
				MassagePanel.writeMessagePanel("Start the Game!");
			}
			else if (e.getSource() == buttonSave) {
				HandlerMessagesPuzzle handMessaPuzzle = new EroarePuzzleMessagesLitere();
				if (handMessaPuzzle.HandleRequest(getDataFromTable())==false){
					MassagePanel.writeMessagePanel("[INFO] Datele pentru Puzzle s-au introdus corect");
					date = getDataFromTable();
					Joc j=Joc.getJocinstance();
					j.setState(JocStates.PUZZLE);
					j.setInitialvalue(getDataFromTable());
					SimulareDesenPanel.textdesen.setText("");
				}
			}
		}
	}
}
